import pygame

import images
from constant import *

class Ball :
    def __init__(self, color, type, number) :
        self.color = color
        self.type = type
        self.number = number
        self.screen_position = self.__findScreenPosition__()
        
    def __findScreenPosition__(self) :
        x, y = 0, 0
        if self.type == BALL_ROBOT :
            x, y = images.BALL_ROBOT_START_X, images.BALL_ROBOT_START_Y
            y += self.number * (images.SPRITE_ROBOT + images.BALL_ROBOT_DIFF)
            return (x, y)
        if self.type == BALL_BOARD :
            x, y = images.BALL_BOARD_START_X, images.BALL_BOARD_START_Y
            x += self.number * (images.SPRITE_BOARD + images.BALL_BOARD_DIFF)
            return (x, y)
        
    def paint(self, screen) :
        if self.type == BALL_ROBOT :
            sprite = images.getImageRobot(self.color)
        elif self.type == BALL_BOARD :
            sprite = images.getImageBoard(self.color)
        else:
            return
        screen.blit(sprite, self.screen_position)
        
    def changeColor(self, direction, max_color) :
        try:
            colors = getColors_constants(max_color)
            index = colors.index(self.color)
            if direction == PREVIOUS :
                index -= 1
            elif direction == NEXT :
                index += 1
            else :
                raise
            index %= len(colors)
            self.color = colors[index]
        except:
            pass
        
    def previousColor(self, max_colors) :
        self.changeColor(PREVIOUS, max_colors)
        
    def nextColor(self, max_colors) :
        self.changeColor(NEXT, max_colors)
        
        
    def downPosition(self) :
        if self.type == images.BALL_BOARD :
            self.screen_position = (self.screen_position[0],self.screen_position[1] + images.SPRITE_BOARD + 2)
